<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\AlertInfo;
use Excel;

class AlertController extends Controller
{
	public function index(Request $request)
	{
		$stationList = ['湖西派出所', '于集派出所', '凤凰派出所', '朱老庄派出所'];
		return view('alert.index', compact(
			'stationList'
		));
	}

	public function addAlert(Request $request){
		$alertInfo = array();
		$alertInfo['own_police_station'       ] = $request->get('own_police_station'       );
		$alertInfo['detail_addr'              ] = $request->get('detail_addr'              );
		$alertInfo['alert_people_name'        ] = $request->get('alert_people_name'        );
		$alertInfo['alert_people_mobile'      ] = $request->get('alert_people_mobile'      );
		$alertInfo['alert_type'               ] = $request->get('alert_type'               );
		$alertInfo['alert_detail_info'        ] = $request->get('alert_detail_info'        );
		$alertInfo['process_police_man_name'  ] = $request->get('process_police_man_name'  );
		$alertInfo['process_police_man_mobile'] = $request->get('process_police_man_mobile');

		AlertInfo::create($alertInfo);
	}

	public function getAlertListSearch(Request $request){
		$policeStation = $request->get('own_police_station');
		$alertType = $request->get('alert_type');

		if($policeStation === NULL){
			$policeStation = AlertInfo::query()
				->select('own_police_station')
				->distinct()
				->get();
		}else{
			$policeStation = [$policeStation];
		}

		if($alertType === NULL){
			$alertType = AlertInfo::query()
				->select('alert_type')
				->distinct()
				->get();	
		}else{
			$alertType = [$alertType];
		}


		$allAlertList = AlertInfo::query()
			->whereIn('alert_type', $alertType)
			->whereIn('own_police_station', $policeStation)
			->orderBy('created_at', 'desc')
			->get()->toArray();

		return view('alert.list', compact(
			'allAlertList'
		));
	}

	public function getAlertInfo(Request $request){
		$alertId = $request->get('alert_id');

		$alertInfo = AlertInfo::query()
			->where('id', '=', $alertId)
			->first()->toArray();
		return $alertInfo;

	}

	public function getStationList(Request $request){
		$stationList = [ 		'湖西派出所', 		'于集派出所', 		'凤凰派出所', 		'朱老庄派出所', 	];
		return $stationList;
	}

	public function getAlertList(Request $request){
		$allAlertList = AlertInfo::query()
			->orderBy('created_at', 'desc')
			->get()->toArray();
		
		$huxiAlertList = AlertInfo::query()
			->where('own_police_station', '=', '湖西派出所')
			->orderBy('created_at', 'desc')
			->get()->toArray();
		$yujiAlertList = AlertInfo::query()
			->where('own_police_station', '=', '于集派出所')
			->orderBy('created_at', 'desc')
			->get()->toArray();		
		$fenghuangAlertList = AlertInfo::query()
			->where('own_police_station', '=', '凤凰派出所')
			->orderBy('created_at', 'desc')
			->get()->toArray();		
		$zulaozhuangAlertList = AlertInfo::query()
			->where('own_police_station', '=', '朱老庄派出所')
			->orderBy('created_at', 'desc')
			->get()->toArray();

		$zhianAlertList = AlertInfo::query()
			->where('alert_type', '=', '治安')
			->orderBy('created_at', 'desc')
			->get()->toArray();
		$xingshiAlertList = AlertInfo::query()
			->where('alert_type', '=', '刑事')
			->orderBy('created_at', 'desc')
			->get()->toArray();
		$otherAlertList = AlertInfo::query()
			->where('alert_type', '=', '其他')
			->orderBy('created_at', 'desc')
			->get()->toArray();			

		Excel::create('policeInfo', function($excel)use($allAlertList, $huxiAlertList, $yujiAlertList, $fenghuangAlertList, $zulaozhuangAlertList, $zhianAlertList, $xingshiAlertList,$otherAlertList){
			$excel->sheet('全部', function($sheet)use($allAlertList){
					$sheet->rows($allAlertList);
				});
			$excel->sheet('湖西派出所', function($sheet)use($huxiAlertList){
					$sheet->rows($huxiAlertList);
				});
			$excel->sheet('于集派出所', function($sheet)use($yujiAlertList){
					$sheet->rows($yujiAlertList);
				});
			$excel->sheet('凤凰派出所', function($sheet)use($fenghuangAlertList){
					$sheet->rows($fenghuangAlertList);
				});
			$excel->sheet('朱老庄派出所', function($sheet)use($zulaozhuangAlertList){
					$sheet->rows($zulaozhuangAlertList);
				});					
			$excel->sheet('类型-治安', function($sheet)use($zhianAlertList){
					$sheet->rows($zhianAlertList);
				});	
			$excel->sheet('类型-刑事', function($sheet)use($xingshiAlertList){
					$sheet->rows($xingshiAlertList);
				});	
			$excel->sheet('类型-其他', function($sheet)use($otherAlertList){
					$sheet->rows($otherAlertList);
				});																							
		})->export('xls');
	}
}
